package com.rc.saas.tenant.service.weixin;

import com.rc.saas.tenant.common.component.UidGenerator;
import com.rc.saas.tenant.common.constant.CommonConstant;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.dto.TransPayWalletResultDto;
import com.rc.saas.tenant.model.member.MemberInfo;
import com.rc.saas.tenant.model.tenant.Tenant;
import com.rc.saas.tenant.model.tenant.TenantSetting;
import com.rc.saas.tenant.service.tenant.TenantService;
import com.rc.saas.tenant.service.tenant.TenantSettingService;
import com.rc.saas.tenant.service.transfer.MemberRefundApplyService;
import com.rc.saas.tenant.service.transfer.TransferOrderService;
import com.rc.saas.tenant.transfers.weixin.WxTransPayBankVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.weixin4j.pay.model.paywallet.TransPayWalletResult;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 参考PayBankComponment类
 * Created by Sven on 2021/12/24
 */
@Service
public class WxPayBankServiceImpl implements WxPayBankService {
    private static Logger logger = LogManager.getLogger(WxPayBankServiceImpl.class);

    @Autowired
    private UidGenerator uidGenerator;

    @Autowired
    private TenantService tenantService;

    @Autowired
    private TransferOrderService transferOrderService;

    @Autowired
    private MemberRefundApplyService memberRefundApplyService;

    @Autowired
    private TenantSettingService tenantSettingService;

    @Override
    public TResponse transPayBank(HttpServletRequest request, Tenant tenant, MemberInfo memberInfo, WxTransPayBankVo transPayBankVo, TenantSetting tenantSetting) {
        //订单总金额，单位为分
        //交易金额默认为人民币交易，接口中参数支付金额单位为【分】，参数值不能带小数。对账单中的交易金额单位为【元】。
        //外币交易的支付金额精确到币种的最小单位，参数值不能带小数点
        BigDecimal targetAmount = transPayBankVo.getAmount().multiply(new BigDecimal("100")).setScale(0, BigDecimal.ROUND_DOWN);
        if (targetAmount.compareTo(BigDecimal.ZERO) <= 0) {
            return TResponse.FAIL("付款金额不合法" + targetAmount);
        }

        //根据不同的运营商简码生成充值订单号
        String outTradeNo = uidGenerator.genWxTransferOutTradeNo(tenant.getTenantPrefix(), memberInfo.getMobile(), CommonConstant.WX_TRANSFER_PREFIX);
        if (outTradeNo == null) {
            return TResponse.FAIL("无法生成付款订单号");
        }

        //生成付款单
        //TransferOrder transferOrder = new TransferOrder();
        //transferOrder.setTenantCode(tenant.getTenantCode());
        //transferOrder.setMemberId(memberInfo.getMemberId());
        //transferOrder.setMemberRealName(transPayBankVo.getRealName());
        //transferOrder.setOutTradeNo(outTradeNo);
        //transferOrder.setRefundApplyId(transPayBankVo.getRefundApplyId());//申请单ID
        //transferOrder.setAmount(transPayBankVo.getAmount());//单位（元）
        //
        ////充值方式  0后台 1公众号 2 APP  3小程序  默认0
        //transferOrder.setTransferType("0");
        //
        ////支付方式  0 微信支付  1支付宝 Z0中行微信 Z1中行支付宝 U银联支付
        //transferOrder.setPaymentType("0");
        //
        ////到帐状态 0未到帐  1已到帐 默认0
        //transferOrder.setStatus("0");
        //transferOrder.setCreatedTime(new Date());
        //transferOrderService.insertReturnId(transferOrder);

        return null;
    }

    @Override
    public TransPayWalletResult transPayBank(TransPayWalletResultDto transPayWalletResultDto) {
        return null;
    }
}
